package com.xx.doublexchat.common.user.dao;

import com.xx.doublexchat.common.common.domain.enums.YesOrNoEnum;
import com.xx.doublexchat.common.user.domain.entity.ItemConfig;
import com.xx.doublexchat.common.user.domain.entity.UserBackpack;
import com.xx.doublexchat.common.user.mapper.UserBackpackMapper;
import com.xx.doublexchat.common.user.service.IUserBackpackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户背包表 服务实现类
 * </p>
 *
 * @author xiaoXuan
 * @since 2024-01-13
 */
@Service
public class UserBackpackDao extends ServiceImpl<UserBackpackMapper, UserBackpack>{

    /**
     * 剩余可改名次数
     * @param uid
     * @param itemId
     * @return
     */
    public Integer getCountByValidItemId(Long uid, Long itemId) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, itemId)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .count();
    }

    /**
     * 获取第一个物品
     * @param uid
     * @param itemId
     * @return
     */
    public UserBackpack getFirstValidaItem(Long uid, Long itemId) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, itemId)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .orderByAsc(UserBackpack::getId)
                .last("limit 1").one();
    }

    /**
     * 使用改名卡
     * @param itemId
     */
    public boolean useItem(Long itemId) {
        return lambdaUpdate()
                .eq(UserBackpack::getId, itemId)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .set(UserBackpack::getStatus, YesOrNoEnum.YES.getStatus()).update();
    }

    /**
     * 通过ItemId和Uid获取用户信息
     * @param uid
     * @param itemIds
     * @return
     */
    public List<UserBackpack> getByItemIds(Long uid, List<Long> itemIds) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .in(UserBackpack::getItemId, itemIds).list();
    }

    /**
     * 通过幂等号去获取信息
     * @param idempotent
     */
    public UserBackpack getByIdempotent(String idempotent) {
        return lambdaQuery()
                .eq(UserBackpack::getIdempotent,idempotent)
                .one();
    }

    /**
     * 通过uIds和itemIds查询背包表信息
     * @param uIds
     * @param itemIds
     * @return
     */
    public List<UserBackpack> getByItemIds(List<Long> uIds, List<Long> itemIds) {
        return lambdaQuery().in(UserBackpack::getUid, uIds)
                .in(UserBackpack::getItemId, itemIds)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .list();
    }
}
